﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using DbProject.Models;
using System.Web.Security;
using FluentSecurity;
using MvcEasyProject.Controllers;
using System.Reflection;
using DbProject;
using MvcEasyProject.App_Start.Security;
using MvcEasyProject;


namespace MvcEasyProject
{
    public class SecurityConfig
    {
        public static void registerSecurityConfiguration(GlobalFilterCollection filters)
        {
            //Uncomment this line to eable custom attrbute security

            WebSecurity.InitializeDatabaseConnection("DefaultConnection", "User", "Id", "UserName", true);
            UserModel CreateAdmin = new UserModel();
            //Tuple<CreateAdmin.UserName.GetType(), CreateAdmin.Password.GetType(), CreateAdmin.Active.GetType(), CreateAdmin.WorkerId.GetType(), CreateAdmin.WorkerId.GetType()> UserDic = new <CreateAdmin.UserName.GetType(), CreateAdmin.Password.GetType(), CreateAdmin.Active.GetType(), CreateAdmin.WorkerId.GetType(), CreateAdmin.WorkerId.GetType()>("", "", true, 1, 1);
            using (var db = new DbProjectContext())
            {
                if (db.Users.FirstOrDefault(u => u.UserName == "SysAdministrator") == null)
                {

                    //CreateAdmin.UserName = "SysAdministrator";
                    //CreateAdmin.WorkerId = 1;
                    //CreateAdmin.Active = true;

                    //CreateAdmin.Password = "123456";
                    //db.Users.Add(CreateAdmin);
                    //db.SaveChanges();
                    WebSecurity.CreateUserAndAccount("SysAdministrator", "123456");
                  
                    Roles.CreateRole("Administrator");
                    Roles.AddUserToRole("SysAdministrator", "Administrator");
                }


            }



            SecurityConfigurator.Configure(configuration =>
            {
                // Let Fluent Security know how to get the authentication status of the current user
                configuration.GetAuthenticationStatusFrom(() => HttpContext.Current.User.Identity.IsAuthenticated);
                configuration.GetRolesFrom(() =>
                {
                    return Roles.GetRolesForUser();
                });



               configuration.ForAllControllers().DenyAnonymousAccess();

                //configuration.For<MixController>().Ignore();

                configuration.For<AccountController>().Ignore();
                configuration.For<HomeController>().Ignore();
                configuration.For<SubmitTimeCardController>().Ignore();
                configuration.For<UsersController>().AddPolicy<AdminOnlyPolicy>();
               // configuration.For<UsersController>().Ignore();


                configuration.ResolveServicesUsing(type =>
                {
                    if (type == typeof(IPolicyViolationHandler))
                    {
                        var types = Assembly
                            .GetAssembly(typeof(MvcApplication))
                            .GetTypes()
                            .Where(x => typeof(IPolicyViolationHandler).IsAssignableFrom(x)).ToList();

                        var handlers = types.Select(t => Activator.CreateInstance(t) as IPolicyViolationHandler).ToList();

                        return handlers;
                    }
                    return Enumerable.Empty<object>();
                });



            });

            GlobalFilters.Filters.Add(new HandleSecurityAttribute(), -1);
        }
    }
}